package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: PREETHI
 * Date: 26/5/13
 * Time: 4:28 PM
 * To change this template use File | Settings | File Templates.
 */
import java.util.*;
public class BookManager {
     bookList booklist ;
    Scanner scan =new Scanner(System.in);

    public BookManager(bookList booklist) {
        this.booklist=booklist;
    }


    public void respond(int i,user loggedInUser)
    {
           if(i==1)
               System.out.println(viewBooks());
           else if(i==2&& loggedInUser!=null)
           {
               String bookName=null;
               int bookID=0;
               System.out.println("Enter the book name or book id:");
               if( scan.hasNextInt() )
               {
                   bookID=scan.nextInt();
                   reserveBook(bookID);
               }
               else
               {
                   bookName=scan.next();
                   reserveBook(bookName);
               }

           }
           else
               System.out.println(MESSAGES.LOGIN_TO_RESERVE);
    }


    public String viewBooks() {
        String display;
         display="Book Name\tBook ID\tAuthor Namee\tCategory\n";
        for(book b:booklist.getBookList())
        {
              display+=b.getBookName()+"\t"+b.getBookID()+"\t\t"+b.getAuthorName()+"\t\t"+b.getCategory()+"\n";
        }
        return display;

    }

    public void reserveBook(int bookID) {

        boolean reserved=false;
        for(book b: booklist.getBookList())
        {
            if(b.getBookID()==bookID && b.getQuantity()>0)
            {
                    System.out.println(MESSAGES.BOOK_RESERVATION_SUCCESSFUL);
                     reserved= true;
            }
        }
        if(reserved==false)
            System.out.println(MESSAGES.BOOK_RESERVATION_UNSUCCESSFUL);


    }


    public  void reserveBook(String bookName) {
        boolean reserved=false;
        for(book b: booklist.getBookList())
        {
            if(b.getBookName().equals(bookName)&& b.getQuantity()>0)
            {
                    System.out.println(MESSAGES.BOOK_RESERVATION_SUCCESSFUL);
                    reserved=true;
            }
        }
        if(reserved==false)
            System.out.println(MESSAGES.BOOK_RESERVATION_UNSUCCESSFUL);
    }



}
